package com.visec.hdy.sync.ftp.code;

import java.net.Socket;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import com.visec.hdy.sync.ftp.config.SyncConfig;
import com.visec.hdy.sync.ftp.config.SyncFile;
import com.visec.hdy.sync.ftp.config.SyncSendConfig;
import com.visec.hdy.transmission.ftp.code.FtpClientInter;
import com.visec.hdy.transmission.socket.config.SocketModel;

public class FtpSendManager extends Thread {
	private SyncSendConfig ftpManagerConfig;
	private ThreadLocal<SyncConfig> threadLocal;
	private SocketModel socketModel;
	private volatile boolean isRun = true;

	protected FtpSendManager() {

	}

	public static FtpSendManager getInstance(SyncSendConfig fmc, SocketModel s) {
		FtpSendManager fm = new FtpSendManager();
		fm.ftpManagerConfig = fmc;
		fm.socketModel = s;
		return fm;
	}

	@Override
	public void run() {
		// TODO Auto-generated method stub
		saveConfig();
		int runCount = 0;
		while (isRun) {
			try (Socket s = new Socket(socketModel.getIp(), socketModel.getPort())) {
				
			} catch (Exception e) {
				// TODO: handle exception
				try {
					Runtime.getRuntime().exec("logger -p local5.info ftp-client-connect-server-error=server ip:"
							+ socketModel.getIp() + ",server port:" + socketModel.getPort());
				} catch (Exception e1) {
					// TODO: handle exception

				}
				continue;
			}

			long currentTimeMillis = System.currentTimeMillis();
			runCount++;

			FtpClientInter ftpClientInter = ftpManagerConfig.getFtpSendClient().getFtpClientInter();
			ftpClientInter.login();

			FtpFileModelHandle ftpFileModelHandle = new FtpFileModelHandle(FtpTransmissionImp.getInstance(threadLocal),
					ftpManagerConfig.getFtpId(), ftpManagerConfig.getFtpSendClient().getSocketClient());

			ftpClientInter.handleAllFiles(ftpFileModelHandle);

			ftpClientInter.logout();

			ExecutorCompletionService<SyncFile> ec = threadLocal.get().getExecutorService();
			for (int i = 0; i < ftpFileModelHandle.getSyncSize(); i++) {
				try {
					ec.take();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			threadLocal.get().getFtpSendClientTool().clear();

			long currentTimeMillis2 = System.currentTimeMillis();

			try {
				Runtime.getRuntime()
						.exec("logger -p local5.info ftp-client-start=ftpId:" + ftpManagerConfig.getFtpId()
								+ ",run count:" + runCount + ",file number:" + ftpFileModelHandle.getSyncSize()
								+ ",start time:" + currentTimeMillis + ",end time:" + currentTimeMillis2);
			} catch (Exception e) {
				// TODO: handle exception

			}

			try {
				Thread.sleep(ftpManagerConfig.getSleepTime());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				// e.printStackTrace();
			} finally {
				if (ftpClientInter != null)
					ftpClientInter.logout();
			}
		}
	}

	protected void saveConfig() {
		FtpSendClientTool fsct = new FtpSendClientTool(ftpManagerConfig.getToolSize(),
				ftpManagerConfig.getFtpSendClient());
		ExecutorService threadPool = Executors.newFixedThreadPool(ftpManagerConfig.getToolSize());
		ExecutorCompletionService<SyncFile> ec = new ExecutorCompletionService<SyncFile>(threadPool);
		SyncConfig fc = new SyncConfig(ec, ftpManagerConfig.getDefaultSize(), ftpManagerConfig.isSave(), fsct);
		threadLocal = new ThreadLocal<SyncConfig>();
		threadLocal.set(fc);
	}

	public synchronized void shutdown() {
		this.isRun = false;
		interrupt();
	}

}
